/*
 * @(#)HtmlFinderType.java
 *
 *
 *
 * ver 1.0 Aug 16, 2007 ggranum
 */
package org.testfire.model;

import java.util.List;
import org.testfire.model.commandtypes.CommandType;
import org.testfire.util.DomUtil;
import org.testfire.translator.TranslatorFactory;
import org.w3c.dom.Element;
import org.w3c.dom.TypeInfo;

/**
 * Due to the fucked up inheritence model of XML, we don't have a well defined heirarchy.
 * The base type is HtmlFinder. HtmlElementType inherits all HtmlFinder's elements by using an additional CHOICE union.
 *
 * So simply, XML Schema forces you to do:
 * A =  {item_a  | item_b}[0, 1]
 * B = A[0,1] & { item_c | item_b}[0,1].
 *
 * When what we want is:
 * A = {item_a | item_b}[1]
 * B = {A | item_c | item_b }[1]
 *
 * This point is worth noting in that a poorly defined file (but conforming to the schema) may cause problems that
 * need to be validated.
 *
 */
public class HtmlFinderType implements TestFireObject
{
    private TestFireObject parent;

    String key;

    String useObjectKey;

    String name;

    FinderType finder;

    HtmlFinderType htmlFinderType;

    private CommandType command;

    private String keyType;

    public HtmlFinderType( Element element )
    {
        this.parse( element );
    }

    

    public void setParent( TestFireObject parent )
    {
        this.parent = parent;
    }

    public TestFireObject getParent()
    {
        return parent;
    }

    public void parse( Element el )
    {
        this.key = el.getAttribute( "key" );
        this.keyType = el.getAttribute( "keyType" );
        this.useObjectKey = el.getAttribute( "useObjectKey" );
        this.name = el.getLocalName();
        /* Should get the object to use here. */
        List<Element> children = DomUtil.getChildElements( el );
        for ( Element child : children )
        {
            TypeInfo typeInfo = child.getSchemaTypeInfo();
            String typeName = typeInfo.getTypeName();
            if ( typeName.equals( "FinderType" )
                || typeInfo.isDerivedFrom( "TestFire", "FinderType", TypeInfo.DERIVATION_EXTENSION ) )
            {
                this.finder = new FinderType( child );
            }
            else if ( typeName.equals( "HtmlElementType" )
                || typeInfo.isDerivedFrom( "TestFire", "HtmlElementType", TypeInfo.DERIVATION_EXTENSION ) )
            {
                this.htmlFinderType = new HtmlFinderType( child );
            }
            else if ( typeInfo.isDerivedFrom( "TestFire", "Command", TypeInfo.DERIVATION_EXTENSION )
                || typeName.equals( "Command" ) )
            {
                this.command = CommandType.createCommand( child );
            }
            else{
                throw new IllegalStateException( "Could not find Schema Type for typeName:" + typeName );
            }
        }


    }



    public Object translate( TestsetType testset )
    {
        return TranslatorFactory.translate(testset, this);
    }

    public String getKey()
    {
        return key;
    }

    public String getUseObjectKey()
    {
        return useObjectKey;
    }

    public String getName()
    {
        return name;
    }

    public FinderType getFinder()
    {
        return finder;
    }

    public HtmlFinderType getHtmlFinderType()
    {
        return htmlFinderType;
    }

    public CommandType getCommand()
    {
        return command;
    }

    public String getKeyType()
    {
        return keyType;
    }
}

// eof: HtmlFinderType.java